﻿using EventHandlerDemo.Domain.Orders.Events;
using EventHandlerDemo.Infrastructure.EFCore;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace EventHandlerDemo.Application.Orders.EventHandlers;

public class OrderCreatedEventHandler : INotificationHandler<OrderCreatedEvent>
{
    private readonly AppDbContext _dbContext;

    public OrderCreatedEventHandler(AppDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public async Task Handle(OrderCreatedEvent notification, CancellationToken cancellationToken)
    {
        var order = await _dbContext.Orders.FirstAsync(o => o.Id == notification.order.Id);
        Console.WriteLine($"EventHandle-{order.Id}-{order.Name}");
        // throw new Exception();
    }
}
